/*
 * vector3D.c
 *
 *  Created on: Jun 23, 2011
 *      Authors: Lane Aasen, Nikholas Gaffney, Michael Rosenberger, Dylan Swiggett
 */

#include <math.h>

typedef struct {
	double x, y, z;
} Vector3D;

Vector3D initVector3D (double x, double y, double z) {
	Vector3D v = {x, y, z};
	return v;
}

double magnitudeVector3D (Vector3D v) { //if this causes you an error due to something like "undefined reference to sqrt", contact Michael and he'll tell you what to do
	return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}

Vector3D scaleVector3D (Vector3D v, double scalar) {
	return initVector3D (v.x * scalar, v.y * scalar, v.z * scalar);
}

void scaleIPVector3D (Vector3D* v, double scalar) {
	v->x *= scalar;
	v->y *= scalar;
	v->z *= scalar;
}

Vector3D normalizeVector3D (Vector3D v){
	double m = magnitudeVector3D(v);
	return initVector3D(v.x / m, v.y / m, v.z / m);
}

Vector3D addVector3D (Vector3D v1, Vector3D v2) {
	Vector3D newV;
	newV.x = v1.x + v2.x;
	newV.y = v1.y + v2.y;
	newV.z = v1.z + v2.z;
	return newV;
}

Vector3D subtractVector3D (Vector3D v1, Vector3D v2) {
	return initVector3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}

double dotProductVector3D (Vector3D v1, Vector3D v2) {
	return v1.x * v2.x + v1.y * v2.y;
}

Vector3D crossProductVector3D (Vector3D v1, Vector3D v2) {
	return initVector3D(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
}

Vector3D duplicateVector3D(Vector3D *v){
	return initVector3D(v->x, v->y, v->z);
}

double sumSideLengthsVector3D(Vector3D v){
	return v.x + v.y + v.z;
}

